package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import io.swagger.models.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Resource
    private NursingPlanMapper nursingPlanMapper;

    @Override
    public List<NursingPlanVo> queryAllPlan() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.queryAllPlan();
        //创建一个新Vo集合
        List<NursingPlanVo> nursingPlanVos = new ArrayList<>();
        //遍历Entity集合,把一个个entity对象转化为Vo对象 ,再装入Vo集合中
        /*List<NursingPlanVo> voList = nursingPlanVos.stream().map(n -> BeanUtil.toBean(n, NursingPlanVo.class))
                .collect(Collectors.toList());*/
        for (NursingPlan nursingPlan : nursingPlans) {
            nursingPlanVos.add(BeanUtil.toBean(nursingPlan, NursingPlanVo.class));
        }
        return nursingPlanVos;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        //把nursingPlanDto转化为实体类
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //先更新nursing_plan
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //再更新中间表
        List<NursingProjectPlan> nursingProjectPlanList = nursingPlan.getNursingProjectPlanList();
        if (nursingProjectPlanList != null && !nursingProjectPlanList.isEmpty()) {
            nursingPlanMapper.insertNursingProjectPlan(nursingProjectPlanList);
        }
    }

    /**
     * 护理计划条件分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.selectByPage(name, status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return nursingPlanVoPageResponse;
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo queryById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.queryById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return nursingPlanVo;
    }

    /**
     * 修改护理项目
     *
     * @param nursingPlanDto
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        nursingPlanMapper.updatePlan(nursingPlan);
        List<NursingProjectPlan> nursingProjectPlanList = nursingPlan.getNursingProjectPlanList();

        /*if (nursingProjectPlanList != null && !nursingProjectPlanList.isEmpty()) {

            nursingPlanMapper.updateProjectPlan(nursingProjectPlanList);

        }*/


    }
}




